#include "SignedMessage.hpp"

using namespace BFT::messages;

SignedMessage::SignedMessage(uint16_t _tag, uint32_t _size, uint32_t _sender)
	:VerifiedMessageBase::VerifiedMessageBase(_tag, _size, computeAuthenticationSize())
{
	sender=_sender;
	int offset = getOffset() + getPayloadSize();
	*(uint32_t*)(buffer.get()+offset) = htonl(sender); //setting the sender
	//fprintf(stdout,"end of signedmessage constructor\n");
}


SignedMessage::SignedMessage(boost::shared_ptr<char> _buffer, uint32_t _buffer_length)
	:VerifiedMessageBase::VerifiedMessageBase(_buffer, _buffer_length)
{
	int offset = getOffset() + getPayloadSize();
	
	//setting the sender
	sender = ntohl( *(uint32_t*)( _buffer.get() + offset )  );
}

void SignedMessage::sign()
{
	
	//just memsetting the signature fiels to 255
	
	memset( (void*)(buffer.get() + startSig()), 255 , SIGNATURE_SIZE);
}

void SignedMessage::printInfo()
{
	VerifiedMessageBase::printInfo();
	fprintf(stdout,"SignedMessage information:\n");
	fprintf(stdout,"Sender:\t%d\n",sender);
	fprintf(stdout,"Signature start index:\t%d\n",startSig());
	fprintf(stdout,"Signature size:\t%d\n",SIGNATURE_SIZE);
	/*
	fprintf(stdout,"Signature:\t");
	for(int i=startSig(); i<SIGNATURE_SIZE; i++)
		fprintf(stdout,"|%02X",buffer.get()[i]);
	fprintf(stdout,"\n");
	*/
}

